//           sales.cc
//  Fri Jan  5 17:14:53 2007
//  Copyright  2007  Min Seok Seo
//  <msseo@oasyss.co.kr>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with main.c; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

#include <sales.h>
#include <iostream>
#include <sstream>
#include <ctime>

using namespace PGSTD;
using namespace pqxx;
using namespace std;

Sales::Sales()
{	
	std::string host = "127.0.0.1";
	std::string port="5432";
	std::string dbname="root";
	std::string user="root";
	
	//strConn = "host="+host+" port="+port+" dbname="+dbname+" user="+user;
	strConn = "dbname=root";
	// 변수 초기화
	Ujname = " ";
	User = " ";
	Ver = "v 2007-01-11";
	Workdate = "00000000";
	Nowdate = "0000년.0월.00일.0요일";
	Time = "00:00:00";
	Total_Person = "0";
	Total_Price = "0";

	SOSOKADDR="대전광역시 유성구";	
	SOSOKTNAME="홍길동";	/* 대표자 */
	SOSOKSANO="987654321";	/* 사업자등록번호 */
	SOSOKTEL="042-888-8888";		/* TEL */
	SOSOKFAX="042-888-8888";		/* FAX */
	
	nowtablnum = "0";
	nowACCT = "0";
	transtablnum = "0";
	transACCT = "0";
	nowBILL = "0";
	transBILL = "0";
	maxACCT = "0";
	maxBILL = "0";

	// 차후 DB로 변경..
	card_name[0] = "BC CARD";
	card_name[1] = "VISA CARD";
	card_name[2] = "LG CARD";
	card_name[3] = "국민 CARD";
	card_name[4] = "삼성 CARD";
	card_name[5] = "DINES CARD";
	card_name[6] = "AMEX CARD";
	card_name[7] = "MASTER CARD";
	card_name[8] = "JCB";
	card_name[9] = "롯데 CARD";
	card_name[10] = "신한 CARD";
	card_name[11] = "외환 CARD";
	card_name[12] = "현대 CARD";
	card_name[13] = "기타 CARD";	
	card_name[14] = "NULL";
	
	detailstr[0] = "손님번호 : ";
	detailstr[1] = "테이블명 : ";
	detailstr[2] = "빌번호 : ";
	detailstr[3] = "인원 : ";
	detailstr[4] = "도착시간 : ";
	detailstr[5] = "상태 : ";
	detailstr[6] = "비고 : ";
	detailstr[7] = "봉사원 : ";
	detailstr[8] = "입력자 : ";
	detailstr[9] = "입력시간 : ";
	detailstr[10] = "확인자 : ";
	detailstr[11] = "확인시간 : ";
	
	CHKINCODE = "0160020001";
	CHKOUTCODE = "0160020002";
	CHKOUTPRICE = "0";
	vatbuff = 0;
	svcbuff = 0;
}

Sales::~Sales()
{
}

void Sales::Init(std::string get_SOSOK, std::string get_USERID)
{
	Ujcd = "0010021800";
	// 유저 정보 업장 정보 받아오기
	connection C(strConn);
	work T(C, "sales");
	std::string SQL;
	SQL = 
	"SELECT \
		OPER_USER_ID, \
		OPER_USER_NAME, \
		OPER_USER_SOSOK \
	FROM \
		TB_OPER_USER \
	WHERE \
		OPER_USER_ID = '"+get_USERID+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();

    User = c[0].as(string());
	Username= c[1].as(string());
    SOSOK = c[2].as(string());    
    T.commit();
    C.disconnect();
}
void Sales::Ujinfo()
{
	// 업장정보 알아오기
	connection C(strConn);
	work T(C, "ujinfo");
	std::string SQL;
	SQL = 
	"SELECT \
		UJCD_CODE_NAME, \
		UJCD_CODE_DATE, \
		UJCD_CODE_POSI \
	FROM \
		P_UJCD_CODE \
	WHERE \
		UJCD_CODE_SOSOK = '"+SOSOK+"'\
		AND UJCD_CODE_UJCD = '"+Ujcd+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();

    Ujname = c[0].as(string());
	Workdate = c[1].as(string());
    T.commit();
    C.disconnect();
}

void Sales::Total_Person_Func()
{
	// 방문객수 알아오기
	connection C(strConn);
	work T(C, "totperson");
	std::string SQL;
	SQL = 
	"SELECT \
		SUM(CHEK_TRAN_PERS) \
	FROM \
		P_CHEK_TRAN \
	WHERE \
		CHEK_TRAN_SOSOK = '"+SOSOK+"'\
		AND CHEK_TRAN_UJCD = '"+Ujcd+"'\
		AND CHEK_TRAN_DATE = '"+Workdate+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();

    Total_Person = c[0].as(string());
    T.commit();
    C.disconnect();
}

void Sales::Total_Price_Func()
{
	// 총수입액 갱신
	connection C(strConn);
	work T(C, "totprice");
	std::string SQL;
	SQL = 
	"SELECT \
		SUM(CASH_TRAN_GTOT) \
	FROM \
		P_CASH_TRAN \
	WHERE \
		CASH_TRAN_SOSOK = '"+SOSOK+"'\
		AND CASH_TRAN_UJCD = '"+Ujcd+"'\
		AND CASH_TRAN_DATE = '"+Workdate+"'";
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();

    Total_Price = sep_thousands(c[0].as(int()));
    T.commit();
    C.disconnect();
}

void Sales::Timer()
{
	time_t rawtime;
	time(&rawtime);
	struct tm * timeinfo = localtime (&rawtime);
	// 영업일
	int wday = timeinfo->tm_wday;
	std::string day;
	switch(wday)
	{
		case 0:
			day = "일요일";
			break;
		case 1:
			day = "월요일";
			break;
		case 2:
			day = "화요일";
			break;
		case 3:
			day = "수요일";
			break;
		case 4:
			day = "목요일";
			break;
		case 5:
			day = "금요일";
			break;
		case 6:
			day = "토요일";
			break;
		default:
			break;
	}
	// 현재 날짜
	std::stringstream nowdatestr;
	nowdatestr << timeinfo->tm_year + 1900 << "년" << timeinfo->tm_mon + 1 << "월" << timeinfo->tm_mday << "일" << day;
	nowdatestr >> Nowdate;

	// 현재 시간
	std::stringstream timestr;
	timestr << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec;
	timestr >> Time;
	
/*
  rawtime = time(NULL) + (24 * 60 * 60);
  timeinfo = localtime(&timer);
*/
	
}

void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = "&")
{
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
		tokens.push_back(str.substr(lastPos, pos - lastPos));
        lastPos = str.find_first_not_of(delimiters, pos);
        pos = str.find_first_of(delimiters, lastPos);
    }
}

std::string Sales::sep_thousands(int i)
{
    const char *locale_name =
    #ifdef WINDOWS
    	"korean";
    #else
    	"ko_KR";
    #endif
    
    ostringstream oss;
    oss.imbue(locale(locale_name));
    oss << i;
    return oss.str();
}
